import type { PropsType, FormDataType, EmitUploadFile } from "../type"
import { useStore } from "@/store"
import http from "@/utils/http"
import { ElMessage } from "element-plus"
import { Ref } from "vue-demi"

function useAddFile(
  props: PropsType,
  emit: EmitUploadFile,
  formData: FormDataType,
  modalFlag: Ref<boolean>
) {
  const store = useStore()
  const addFile = async () => {
    if (!formData.name) {
      return
    }

    const res = await http.post("/file/addFile", {
      userId: store.state.user.id,
      isDir: props.isDir,
      name: formData.name,
      editable: formData.editable,
      parentId: props.parentId,
      content: "",
    })

    if (res.code === 1) {
      ElMessage.success("添加成功")
      modalFlag.value = false
      emit("updateFile", props.parentId, props.handle)
    }
  }

  return { addFile }
}

export default useAddFile
